﻿using System.Collections.Generic;

namespace Intyme.Common.DataModel {
    /*
        public class WorkListMachines
        {
            // array of pair MachineID , index <0... number of Machine in DataModel.Machines.Machines>
            public Dictionary<int, int> ListMashines = null;
            public Dictionary<int, PartialOperationOnSchedule> ListOperationOnSchedule = null;

            public class OperationTime_Pair 
            {
                public int start = -2;
                public int end = -2;

                public OperationTime_Pair()
                {
                    start = -1;
                    end = -1;
                }
            }




            public WorkListMachines()
            {
                if ( ListMashines == null ) 
                {
                    ListMashines = new Dictionary<int, int>();
                }

                if ( ListOperationOnSchedule == null )
                {
                    ListOperationOnSchedule = new Dictionary<int,PartialOperationOnSchedule>();
                }

            }

            ~WorkListMachines()
            {
                if (ListMashines != null)
                {
                    ListMashines.Clear();
                    ListMashines = null;
                }

                if (ListOperationOnSchedule != null)
                {
                    ListOperationOnSchedule.Clear();
                    ListOperationOnSchedule = null;
                }

            }
        }
    */

    //     public class WorkListComponents
    //     {
    // 
    //     }


    public class DataModel {
        public OrdersClass Orders;
        public ComponentsClass Components;
        public MachinesClass Machines;
        public ScheduledMachinesClass ScheduledMachines;


        // dictionary MachineID,index;
        public Dictionary<int, int> WorkCastingMachines;
        // dictionary index,MachineID;
        public Dictionary<int, int> WorkCastingMachinesReverse;

        // dictionary MachineID,index;
        public Dictionary<int, int> WorkCNC_ManualMachines;
        // dictionary index,MachineID;
        public Dictionary<int, int> WorkCNC_ManualMachinesReverse;

        // dictionary MachineID,index;
        public Dictionary<int, int> ManualWorkstations;
        // dictionary index,MachineID;
        public Dictionary<int, int> ManualWorkstationsReverse;

        // dictionary index,OperationID;
        public Dictionary<int, int> WorkCastingOperations;
        // dictionary index, OrderID
        public Dictionary<int, int> WorkCastingOrders;

        // dictionary index,OperationID;
        public Dictionary<int, int> WorkCNC_ManualOperations;
        // dictionary index, OrderID
        public Dictionary<int, int> WorkCNC_ManualOrders;

        // dictionary index,OperationID;
        public Dictionary<int, int> WorkManualOperations;
        // dictionary index, OrderID
        public Dictionary<int, int> WorkManualOrders;

        public DataModel() {
            Orders = new OrdersClass();
            Components = new ComponentsClass();
            Machines = new MachinesClass();
            ScheduledMachines = new ScheduledMachinesClass();

            WorkCastingMachines = new Dictionary<int, int>();
            WorkCastingMachinesReverse = new Dictionary<int, int>();
            WorkCNC_ManualMachines = new Dictionary<int, int>();
            WorkCNC_ManualMachinesReverse = new Dictionary<int, int>();
            ManualWorkstations = new Dictionary<int, int>();
            ManualWorkstationsReverse = new Dictionary<int, int>();

            WorkCastingOperations = new Dictionary<int, int>();
            WorkCastingOrders = new Dictionary<int, int>();
            WorkCNC_ManualOperations = new Dictionary<int, int>();
            WorkCNC_ManualOrders = new Dictionary<int, int>();
            WorkManualOperations = new Dictionary<int, int>();
            WorkManualOrders = new Dictionary<int, int>();
        }

        ~DataModel() {
            if (Orders != null) {
                Orders = null;
            }

            if (Components != null) {
                Components = null;
            }

            if (Machines != null) {
                Machines = null;
            }

            if (ScheduledMachines != null) {
                ScheduledMachines = null;
            }

            if (WorkCastingMachines != null) {
                WorkCastingMachines.Clear();
                WorkCastingMachines = null;
            }

            if (WorkCastingMachinesReverse != null) {
                WorkCastingMachinesReverse.Clear();
                WorkCastingMachinesReverse = null;
            }

            if (WorkCNC_ManualMachines != null) {
                WorkCNC_ManualMachines.Clear();
                WorkCNC_ManualMachines = null;
            }

            if (WorkCNC_ManualMachinesReverse != null) {
                WorkCNC_ManualMachinesReverse.Clear();
                WorkCNC_ManualMachinesReverse = null;
            }

            if (ManualWorkstations != null) {
                ManualWorkstations.Clear();
                ManualWorkstations = null;
            }

            if (ManualWorkstationsReverse != null) {
                ManualWorkstationsReverse.Clear();
                ManualWorkstationsReverse = null;
            }

            if (WorkCastingOperations != null) {
                WorkCastingOperations.Clear();
                WorkCastingOperations = null;
            }

            if (WorkCastingOrders != null) {
                WorkCastingOrders.Clear();
                WorkCastingOrders = null;
            }

            if (WorkCNC_ManualOperations != null) {
                WorkCNC_ManualOperations.Clear();
                WorkCNC_ManualOperations = null;
            }

            if (WorkCNC_ManualOrders != null) {
                WorkCNC_ManualOrders.Clear();
                WorkCNC_ManualOrders = null;
            }

            if (WorkManualOperations != null) {
                WorkManualOperations.Clear();
                WorkManualOperations = null;
            }
            if (WorkManualOrders != null) {
                WorkManualOrders.Clear();
                WorkManualOrders = null;
            }
        }

        public OperationClass GetOperationByID(int ID, ComponentsClass Components) {
            foreach (KeyValuePair<int, ComponentClass> aComponent in Components.Components) {
                foreach (KeyValuePair<int, OperationClass> operation_pair in aComponent.Value.Operations) {
                    if (operation_pair.Key == ID)
                        return operation_pair.Value;
                }

                //OperationClass Operaion = GetOperationByID(ID, aComponent.Value. 
            }

            return null;
        }

        //public ComponentClass GetComponentByOperationID(int ID, ComponentsClass Components)
        //{
        //    foreach (KeyValuePair<int, ComponentClass> aComponent in Components.Components)
        //    {
        //        foreach (KeyValuePair<int, OperationClass> operation_pair in aComponent.Value.Operations)
        //        {
        //            if (operation_pair.Key == ID)
        //                return aComponent.Value;
        //        }

        //        //OperationClass Operaion = GetOperationByID(ID, aComponent.Value. 
        //    }

        //    return null;
        //}

        public void DeleteAllData() {
            if (Orders != null) {
                Orders = null;
            }

            if (Components != null) {
                Components = null;
            }

            if (Machines != null) {
                Machines = null;
            }

            if (ScheduledMachines != null) {
                ScheduledMachines = null;
            }
        }

        public string AddComponent(ComponentClass newComponent) {
            if (Components != null) {
                Components.AddComponent(newComponent);
            } else {
                //error
            }


            return string.Empty;
        }

        public string AddMachine(MachineClass newMachine) {
            if (Machines != null) {
                Machines.AddMachine(newMachine);
            } else {
                //error
            }


            return string.Empty;
        }

        public string AddOrder(OrderClass newOrder) {
            if (Orders != null) {
                Orders.AddOrder(newOrder);
            } else {
                //error
            }

            return string.Empty;
        }
    }
}